package com.etalk.crm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.*;
import com.etalk.crm.service.PersonService;
import com.etalk.crm.service.SchoolService;
import com.etalk.crm.service.StudentInfoService;
import com.etalk.crm.service.UserService;
import com.etalk.crm.utils.DateUtil;
import com.etalk.crm.utils.OssGetObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @Auther: James
 * @Date: 2019/3/6 15:36
 * @Description:
 */
@Controller
@RequestMapping(value = "/personCtl",produces = "application/json; charset=utf-8")
public class PersonCtl {
    @Resource
    private PersonService personService;

    @Resource
    private UserService userService;

    @Resource
    private SchoolService schoolService;

    @Resource
    private StudentInfoService studentInfoService;

    /**
     * 由于ajax无法直接导出excel,所以第一次把请求生成的ExcelParam缓存起来,然后前端再次window.open(url);
     */
    private static Map<String, PersonParamter> excelParamCache = new ConcurrentHashMap<>();


    /*
     * @Author James
     * @Description //TODO
     * @Date 14:27 2019/4/1
     * @Param 学员基本信息列表
     * @return
     **/
    @RequestMapping(value = "/personPageInfo",method = RequestMethod.GET)
    public String personPageInfo(){
        return "student/student_list";
    }
    
    
    /*
     * @Author James
     * @Description 查询条件 列表
     * @Date 16:24 2019/3/7
     * @Param 
     * @return 
     **/
    @RequestMapping(value = "/searchCriteria",method = RequestMethod.POST)
    @ResponseBody
    public String storesTypeList(HttpSession session){
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("code",1);
        if(Integer.parseInt(session.getAttribute("storesId").toString()) == 1){
            //店铺 一二级 分类  查询 所有的
            List<Map<String,Object>> storesTypeList = personService.searchSelectListByStuList(null);
            jsonMap.put("storesTypeList",storesTypeList);
        }else{
            Stores store = personService.selectStoreInfoById(Integer.parseInt(session.getAttribute("storesId").toString()));
            jsonMap.put("store",store);
        }
        //ssc sec 列表
        List<User>sscAndSecList = userService.sscAndSecList();
        jsonMap.put("sscAndSecList",sscAndSecList);
        //bcc 列表
        List<User> bccList = userService.bccList();
        jsonMap.put("bccList",bccList);

        //学管师 列表
        List<User> ltList = userService.ltList();
        User user=new User();
        user.setId(0);
        user.setLoginName("无学管师");
        ltList.add(0,user);
        jsonMap.put("ltList",ltList);

        //活动列表
        List<Map<String, Object>> promotionList = personService.selectExperienceInfo(1);
        jsonMap.put("promotionList",promotionList);
        //渠道列表
        List<Map<String, Object>> channelInfoList = personService.searchChannelInfo(null);
        jsonMap.put("channelInfoList",channelInfoList);
        //客户属性列表
        List<Map<String, Object>> revisitList = personService.stateListMap(null,null);
        jsonMap.put("revisitList",revisitList);
        //店铺列表
        List<Stores > storesList = personService.searchStores();
        jsonMap.put("storesList",storesList);

        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 获取 cc
     * @Date 10:29 2019/3/8
     * @Param storesStr 多个店铺id字符串
     * @return
     **/
    @RequestMapping(value = "/getCcList",method = RequestMethod.POST)
    @ResponseBody
    public String getCcList(HttpSession session, String storesStr){
        Map<String,Object> jsonMap = new HashMap<>();
        //根据店铺获取cc列表
        Map<String,Object> map = new HashMap<>();
        if(storesStr != null && storesStr.trim() != ""){
            String storesArray[] = storesStr.split(",");
            List<Integer> storesList = new ArrayList<>();
            for(String str : storesArray){
                storesList.add(Integer.parseInt(str));
            }
            map.put("stroresIds",storesList);
        }
        List<User> ccList = userService.ccList(session,map);
        jsonMap.put("ccList",ccList);
        jsonMap.put("code",1);
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 初始化学校
     * @Date 10:29 2019/3/8
     * @Param schoolName 学校名字 schoolId 学校id pageNumber 页码
     * @return 
     **/
    @RequestMapping(value = "/showSchoolList",method = RequestMethod.POST)
    @ResponseBody
    public String showSchoolList(String schoolName, Integer schoolId,Integer pageNumber, Integer pageSize){
        Map<String,Object> jsonMap = new HashMap<>();
        PageInfo<Map<String, Object>> pageInfo = schoolService.searchSchoolList(schoolName,schoolId,pageNumber,pageSize);
        jsonMap.put("list", pageInfo.getList());
        jsonMap.put("totalRow", pageInfo.getTotal());
        return JSONObject.toJSONString(jsonMap);
    }


    /**
     * @Author James
     * @Description 学员基本信息列表
     * @Date 15:12 2019/3/8
     * @Param personParamter 搜素条件bean
     * @return
     **/
    @RequestMapping(value = "/getStudentList",method = RequestMethod.POST)
    @ResponseBody
    public String getStudentList(@RequestBody PersonParamter personParamter, HttpSession session){
        Map<String,Object> jsonMap = new HashMap<>(6);
        Map<String,Object> resultMap = personService.getStudentList(personParamter);

        jsonMap.put("studentList",resultMap.get("personList"));
        //总条数
        jsonMap.put("total",resultMap.get("total"));
        //角色
        jsonMap.put("roleId",Integer.parseInt(session.getAttribute("roleId").toString()));
        //当前用户
        jsonMap.put("userId",Integer.parseInt(session.getAttribute("userid").toString()));
        //角色所属店铺
        jsonMap.put("storesId",Integer.parseInt(session.getAttribute("storesId").toString()));
        jsonMap.put("code",1);
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 抢单
     * @param session
     * @param id 学员id
     * @param isSms 是否发送短信给用户
     * @param contactTime 联系用户时间
     * @return 结果
     */
    @RequestMapping(value = "/setCC",method = RequestMethod.POST)
    @ResponseBody
    public String setCC(HttpSession session,int id, int isSms,String contactTime,String studentLogin,String phone){
        Map<String,Object> jsonMap = new HashMap<>(1);
        int maxRobCase = StringUtil.isNotEmpty(session.getAttribute("robCase").toString()) ? Integer.parseInt(session.getAttribute("robCase").toString()) : 0;
        int result = personService.setCc(id,Integer.parseInt(session.getAttribute("userid").toString()),
                Integer.parseInt(session.getAttribute("storesId").toString()), 1, maxRobCase,phone,isSms,contactTime,studentLogin);
        if(result > 0){
            jsonMap.put("code",1);
            //抢单日志插入
            GrabOrderLogs gl = new GrabOrderLogs();
            gl.setPersonId(id);
            gl.setNewCcSsc(Integer.parseInt(session.getAttribute("userid").toString()));
            gl.setOperationEvent("抢单成功");
            gl.setDescription("学生管理->学生基本信息->抢单");
            gl.setTypeId(1);
            List<GrabOrderLogs> list = GrabOrderLogsList(session,gl);
            personService.addGrabOrderLogs(list);
        }else if(result == -1){
            jsonMap.put("code",-1); //抢单次数达到上限
        }else{
            jsonMap.put("code",0); //抢单失败
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 日志公共方法
     * @Date 14:40 2019/4/1
     * @Param gl 转单日志bean
     * @return
     **/
    public List<GrabOrderLogs> GrabOrderLogsList(HttpSession session,GrabOrderLogs gl) {
        gl.setAddUserId(Integer.parseInt(session.getAttribute("userid").toString()));
        gl.setAddUserName(session.getAttribute("loginname").toString());
        List<GrabOrderLogs> list = new ArrayList<GrabOrderLogs>();
        list.add(gl);
        return list;
    }


    /*
     * @Author James
     * @Description 获取 用户登录 token
     * @Date 11:09 2019/3/20
     * @Param id 学员id
     * @return
     **/
    @RequestMapping(value = "/getTokenString",method = RequestMethod.POST)
    @ResponseBody
    public String getTokenString(int id){
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("code",1); //抢单次数达到上限
        jsonMap.put("student",studentInfoService.searchStudentInfo(id));
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 转单 cc 列表
     * @Date 11:09 2019/3/20
     * @Param  flag 区分转单还是变更cc
     * @return
     **/
    @RequestMapping(value = "/ccList",method = RequestMethod.POST)
    @ResponseBody
    public String ccList(HttpSession session,int flag){
        List<User> ccList = null;
        Map<String,Object> jsonMap = new HashMap<>();
        List<Integer> listuser = new ArrayList<>();
		listuser.add(2);
		listuser.add(12);
		listuser.add(14);
        jsonMap.put("code",1);
        if(flag == 1){
			ccList = userService.selectUserNameAndIdList(Integer.parseInt(session.getAttribute("storesId").toString()),listuser);
		}else{
			ccList = userService.selectUserNameAndIdList(null,listuser);
		}
        jsonMap.put("ccList",ccList);
        return JSONObject.toJSONString(jsonMap);
    }


    /*
     * @Author James
     * @Description 转单 或者 更改 cc
     * @Date 15:58 2019/3/20
     * @Param ccId 课程顾问id
     * @Param personId 学员id
     * @Param type 区分 转单还是 变更cc
     * @return
     **/
    @RequestMapping(value = "/changeCc",method = RequestMethod.POST)
    @ResponseBody
    public String changeCc(int ccId,int personId,Integer type){
        Map<String,Object> jsonMap = new HashMap<>();
        int result = personService.changeCc(personId,ccId, type);
        if (result > 0) {
            jsonMap.put("code", 1);
        } else {
            //变更失败
            jsonMap.put("code", 0);
        }
        return JSONObject.toJSONString(jsonMap);
    }


    /*
     * @Author James
     * @Description 删除 学员
     * @Date 15:58 2019/3/20
     * @Param personId 学员表 id
     * @return
     **/
    @RequestMapping(value = "/delPerson",method = RequestMethod.POST)
    @ResponseBody
    public String delPerson(int personId){
        Map<String,Object> jsonMap = new HashMap<>();
        int result = personService.delPerson(0,personId);
        if(result > 0){
             jsonMap.put("code", 1);
        }else{
             jsonMap.put("code", 0);
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description cc bcc 列表
     * @Date 15:58 2019/3/20
     * @Param
     * @return
     **/
    @RequestMapping(value = "/getCcOrBccList",method = RequestMethod.POST)
    @ResponseBody
    public String getCcOrBccList (HttpSession session){
        Map<String,Object> jsonMap = new HashMap<>();
        //查询cc列表
        List<User> ccList = null;
        List<Integer> roleList = new ArrayList<>();
        roleList.add(2);
        roleList.add(10);
        roleList.add(12);
        roleList.add(14);
        if (Integer.parseInt(session.getAttribute("roleId").toString()) == 1 || Integer.parseInt(session.getAttribute("roleId").toString()) == 14) {
            ccList = userService.selectUserNameAndIdList(null,roleList);//查询cc
        } else if (Integer.parseInt(session.getAttribute("roleId").toString()) == 12) {
            ccList = userService.selectUserNameAndIdList(Integer.parseInt(session.getAttribute("storesId").toString()),roleList);
        }
        jsonMap.put("ccList", ccList);
        List<User> bccList = null;
        roleList.clear();
        roleList.add(19);
        bccList = userService.selectUserNameAndIdList(null,roleList);//查询cc
        jsonMap.put("bccList", bccList);
        List<User> sscList = null;
        roleList.clear();
        roleList.add(8);
        roleList.add(9);
        roleList.add(15);
        sscList = userService.selectUserNameAndIdList(null,roleList);//查询cc
        jsonMap.put("code", 1);
        jsonMap.put("sscList", sscList);
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     * 批量转移页面
     * @return 多个学员id 字符串
     */
    @RequestMapping(value = "/toCcSscBccList",method = RequestMethod.GET)
    public String toCcSscBccList(Model model,String strIds){
        model.addAttribute("strIds",strIds);
        return "student/cc_ssc_bcc_list";
    }
    
    /*
     * @Author James
     * @Description 批量转移
     * @Date 11:07 2019/3/25
     * @Param strIds 多个学员id 字符串
     * @Param ccId 要变更的cc
     * @Param sscId 要变更的ssc
     * @Param sscId 要变更的bcc约课顾问
     * @return
     **/
    @RequestMapping(value = "/batchTransfer",method = RequestMethod.POST)
    @ResponseBody
    public String batchTransfer(String strIds, Integer ccId, Integer sscId,Integer bccId){
        Map<String,Object> jsonMap = new HashMap<>(1);
        int result = personService.batchTransfer(strIds,ccId,sscId,bccId);
        if(result > 0){
            jsonMap.put("code", 1);
        }else{
            jsonMap.put("code", 0);
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 添加学员
     * @Date 11:07 2019/3/25
     * @Param studentInfo 学员基本信息
     * @return
     **/
    @RequestMapping(value = "/addPersonInfo",method = RequestMethod.POST)
    @ResponseBody
    public String addPersonInfo(@RequestBody StudentInfo studentInfo) throws IOException {
        Map<String,Object> jsonMap = new HashMap<>();
        int result = personService.addStuInfo(studentInfo);
        if(result  > 0){
            jsonMap.put("code",1);
        }else if(result == -1){
            //电话号码 或者 用户名已被注册
            jsonMap.put("code",-1);
        }else{
            //添加失败
            jsonMap.put("code",0);
        }
        return JSONObject.toJSONString(jsonMap);
    }


    /*
     * @Author James
     * @Description 申请奖学金
     * @Date 10:24 2019/3/27
     * @Param personId 学员id
     * @return
     **/
    @RequestMapping(value = "/addScholarship",method = RequestMethod.POST)
    @ResponseBody
    public String addScholarship(HttpSession session,int personId){
        Map<String,Object> jsonMap = new HashMap<>();
        if(personId > 0){
            StudentScholarship record = new StudentScholarship();
            record.setPersonId(personId);
            record.setScholarship(new BigDecimal(1800));
            record.setCreator(session.getAttribute("loginname").toString());
            record.setState(1);
            record.setAvailableTime(DateUtil.addTimeToMin(DateUtil.getCurrentDatetime(),5));
            record.setIsPay(0);
            //添加申请 记录
            int num = personService.insertScholarship(record);
            if (num > 0){
                //成功
                jsonMap.put("code", 1);
            }else {
                jsonMap.put("code", 0);
            }
        }else{
            //参数错误
            jsonMap.put("code",-1);
        }
        return JSONObject.toJSONString(jsonMap);
    }


    /*
     * @Author James
     * @Description 查看奖学金金额
     * @Date 10:36 2019/3/27
     * @Param personId 学员 id
     * @return
     **/
    @RequestMapping(value = "/getScholarship",method = RequestMethod.POST)
    @ResponseBody
    public String getScholarship(int personId){
        Map<String,Object> jsonMap = new HashMap<>();
        if (personId > 0) {
            Map<String,Object> resultMap = personService.selectStudentScholarship(personId);
            if (resultMap!=null){
                jsonMap.put("code", 1);
                jsonMap.put("scholarship", new BigDecimal(resultMap.get("scholarship").toString()).stripTrailingZeros().toPlainString());
                jsonMap.put("createTime", resultMap.get("createTime").toString());
            }else {
                jsonMap.put("code", 0);
            }
        }else {
            jsonMap.put("code", -1);
        }
        return JSONObject.toJSONString(jsonMap);
    }


    /*
     * @Author James
     * @Description 查看奖学金金额
     * @Date 10:39 2019/3/27
     * @Param personId 学员 id
     * @return
     **/
    @RequestMapping(value = "/payMoney",method = RequestMethod.POST)
    @ResponseBody
    public String payMoney(int personId){
        Map<String,Object> jsonMap = new HashMap<>();
        if (personId > 0) {
            int num = personService.updateStateByScholarshipByPersonId(personId);
            if (num > 0){
                jsonMap.put("code", 1);
            }else {
                jsonMap.put("code", 0);
            }
        }else {
            //参数错误
            jsonMap.put("code", -1);
        }
        return JSONObject.toJSONString(jsonMap);
    }

    @GetMapping(value = "/teacher/info/{teacherId}/{studentId}")
    public String getTeacherInfo(Model model, @PathVariable("teacherId") int teacherId,@PathVariable("studentId") int studentId){
        TeacherInfoResult result=personService.searchTeacherInfoByPersonId(teacherId,studentId);
        model.addAttribute("teacher",result);
        return "teacher/teacher_card_info";
    }
    
    /*
     * @Author James
     * @Description 查看证书
     * @Date 11:27 2019/3/27
     * @Param  name  学员名
     * @Param  money  奖学金
     * @Param  dates  颁发日期
     * @return
     **/
    @RequestMapping(value = "/cert",method = RequestMethod.GET)
    public String cert(Model model,String name,String money,String dates){
        model.addAttribute("name",name);
        model.addAttribute("money",money);
        model.addAttribute("dates",dates);
        return "scholarshipCert/cert";
    }

    @RequestMapping(value = "/tea/training/record/list",method = {RequestMethod.POST, RequestMethod.GET})
    public String getTeacherTrainingRecordList(Model model,Integer[] listTeaId ,String[] listFileName,Integer pageNum){
        if (StringUtils.isEmpty(pageNum)){
            pageNum=1;
        }
        PageInfo<Map<String,String>> pageInfo=personService.searchTeacherTrainingRecordByPage(listTeaId,listFileName,pageNum,20);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("listTea",personService.searchTeacherList());
        model.addAttribute("listFile", OssGetObject.getTeaTrainingFiles());
        model.addAttribute("listTeaId",listTeaId);
        model.addAttribute("listFileName", listFileName);
        return "teacher/teacher_training_record";
    }

    /**
     * 导出学员基本信息
     * @return
     * @Param  key  导出参数map 的key
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel")
    public ResponseEntity<byte[]> exportExcel(HttpSession session,String key) throws Exception{
        PersonParamter excelParam = (PersonParamter) session.getAttribute(key);
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("学员基本信息表");
        createTitle(workbook,sheet);
        List<StudentInfo> rows = personService.exportPersonList(excelParam);
        //设置日期格式
        XSSFCellStyle style = workbook.createCellStyle();
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd hh:mm"));
        //新增数据行，并且设置单元格数据
        int rowNum=1;
        for(StudentInfo stu : rows){
            XSSFRow row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(rowNum);
            row.createCell(1).setCellValue(stu.getStoresName());
            row.createCell(2).setCellValue(stu.getLoginName());
            row.createCell(3).setCellValue(stu.getCnName());
            row.createCell(4).setCellValue(stu.getEnName());
            row.createCell(5).setCellValue(stu.getCcLogin());
            row.createCell(6).setCellValue(stu.getSscLogin());
            row.createCell(7).setCellValue(DateUtil.format(stu.getRegisterTime(), "yyyy-MM-dd HH:mm:ss"));
            row.createCell(8).setCellValue(stu.getChannelName());
            row.createCell(9).setCellValue(stu.getQrCode());
            row.createCell(10).setCellValue(stu.getRevisitName());
            row.createCell(11).setCellValue(stu.getSchoolName());
            row.createCell(12).setCellValue(stu.getGrade());
            row.createCell(13).setCellValue(stu.getPromotionName());
            row.createCell(14).setCellValue(DateUtil.format(stu.getReleaseTime(), "yyyy-MM-dd HH:mm:ss"));
            row.createCell(15).setCellValue(stu.getState()==1?"有效":(stu.getState()==2?"无效":(stu.getState()==0?"已删除":"")));
            row.createCell(16).setCellValue(stu.getRemark());
            rowNum++;
        }
        //表名
        String fileName = "student_"+DateUtil.getDatetime()+".xls";
        //浏览器下载excel
        return buildExcelDocument(fileName,key,workbook);
    }

    /**
     * @Author James
     * @Description 导出参数缓存
     * @Date 14:59 2019/4/1
     * @Param excelParam 导出参数条件
     * @return
     **/
    @RequestMapping(value = "/getExcelParam")
    @ResponseBody
    public String getExcelParam(HttpSession session,@RequestBody PersonParamter excelParam) {
        Assert.notNull(excelParam,"参数不能为空");
        String key = UUID.randomUUID().toString();
        //excelParamCache.put(key,excelParam);
        session.setAttribute(key,excelParam);
        Map<String,Object> jsonMap = new HashMap<>(2);
        jsonMap.put("key",key);
        jsonMap.put("code",1);
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *创建表头
     * @param workbook
     * @param sheet
     */
    private void createTitle(XSSFWorkbook workbook,XSSFSheet sheet){
        XSSFRow row = sheet.createRow(0);
        //设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度
        sheet.setColumnWidth(1,12*256);
        sheet.setColumnWidth(3,17*256);
        sheet.setColumnWidth(6,17*256);
        sheet.setColumnWidth(7,19*256);
        sheet.setColumnWidth(9,17*256);
        sheet.setColumnWidth(10,17*256);
        sheet.setColumnWidth(11,17*256);
        sheet.setColumnWidth(12,17*256);

        //设置为居中加粗
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setFont(font);

        XSSFCell cell;
        cell = row.createCell(0);
        cell.setCellValue("序号");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue("门店");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("登录名");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("中文名");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("英文名");
        cell.setCellStyle(style);

        cell = row.createCell(5);
        cell.setCellValue("课程顾问");
        cell.setCellStyle(style);

        cell = row.createCell(6);
        cell.setCellValue("SSC");
        cell.setCellStyle(style);

        cell = row.createCell(7);
        cell.setCellValue("注册时间");
        cell.setCellStyle(style);

        cell = row.createCell(8);
        cell.setCellValue("客户渠道");
        cell.setCellStyle(style);

        cell = row.createCell(9);
        cell.setCellValue("推荐人");
        cell.setCellStyle(style);

        cell = row.createCell(10);
        cell.setCellValue("客户属性");
        cell.setCellStyle(style);

        cell = row.createCell(11);
        cell.setCellValue("收集地点");
        cell.setCellStyle(style);

        cell = row.createCell(12);
        cell.setCellValue("年级");
        cell.setCellStyle(style);

        cell = row.createCell(13);
        cell.setCellValue("活动名称");
        cell.setCellStyle(style);

        cell = row.createCell(14);
        cell.setCellValue("预约时间");
        cell.setCellStyle(style);

        cell = row.createCell(15);
        cell.setCellValue("状态");
        cell.setCellStyle(style);

        cell = row.createCell(16);
        cell.setCellValue("备注");
        cell.setCellStyle(style);
    }

     /**
     * 浏览器下载excel
     * @param filename 文件名
     * @param workbook excel工作表名
     * @throws Exception 错误
     */
     private ResponseEntity<byte[]> buildExcelDocument(String filename, String key, XSSFWorkbook workbook) throws Exception{
         HttpHeaders headers=new HttpHeaders();
         headers.add("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename, "utf-8")+ ".xls");
         headers.add("Content-Type","application/binary;charset=UTF-8");
         ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            baos.flush();
        }catch (Exception e){

            //清空 集合
            excelParamCache.remove(key);
        }finally {
            workbook.close();
            if (baos != null) {
                baos.close();
            }
            //清空 参数集合
            excelParamCache.remove(key);
        }
         return new ResponseEntity<>(baos.toByteArray(), headers, HttpStatus.CREATED);
    }
}
